local love = require 'love'

local setColor = love.graphics.setColor
local polygon = love.graphics.polygon

local width = Width
local height = Height

local deckTriangle = {
    w = 0,
    h = 0,
    xPoint = {
        x = 0,
        y = 0,
    },
    yPoint = {
        x = 0,
        y = 0,
    },
    zPoint = {
        x = 0,
        y = 0,
    },
    cPoint = {
        x = 0,
        y = 0,
    },
    segments = 0,
    colors = {
        normal = {149/255, 165/255, 166/255}, -- 蓝灰色
        isHovered = {179/255, 198/255, 199/255}, -- 更浅的蓝灰色
        isPressed = {108/255, 122/255, 137/255}, -- 更深的蓝灰色
        desk = {0.0745, 0.0627, 0.0667, 1} -- 黑色
    },
    isHovered = false,
    isPressed = false,
}

local cyCurve, czCurve
local vertices = {}
local vertices1 = {}

-- 计算点p关于线段ab的对称点
function love.calculateSymmetricPoint(p, a, b)
    -- 线段ab的向量
    local abx = b.x - a.x
    local aby = b.y - a.y
    -- 线段ab的长度平方
    local abLen2 = abx * abx + aby * aby

    -- 计算点p到线段ab的投影比例t
    local t = ((p.x - a.x) * abx + (p.y - a.y) * aby) / abLen2

    -- 计算投影点q
    local q = {
        x = a.x + t * abx,
        y = a.y + t * aby
    }

    -- 计算对称点 c = q + (q - p) = 2q - p
    return {
        x = 2 * q.x - p.x,
        y = 2 * q.y - p.y
    }
end

-- 生成从点start到点end的曲线点列表，isCounterClockwise控制弯曲方向
local function generateCurve(start, finish, isCounterClockwise)
    local points = {}
    local controlPoint = {}

    -- 计算线段中点
    local midX = (start.x + finish.x) / 2
    local midY = (start.y + finish.y) / 2

    -- 计算垂直于线段的方向向量（逆时针方向）
    local dx = finish.x - start.x
    local dy = finish.y - start.y
    local perpX, perpY
    if isCounterClockwise then
        perpX = -dy
        perpY = dx
    else
        perpX = dy
        perpY = -dx
    end

    -- 归一化垂直向量
    local len = math.sqrt(perpX * perpX + perpY * perpY)
    perpX = perpX / len
    perpY = perpY / len

    -- 设置控制点偏移距离（可调整此值改变弯曲程度）
    local offsetDistance = math.sqrt(dx * dx + dy * dy) * 0.3
    controlPoint.x = midX + perpX * offsetDistance
    controlPoint.y = midY + perpY * offsetDistance

    -- 使用贝塞尔曲线生成曲线上的点
    for i = 0, deckTriangle.segments do
        local t = i / deckTriangle.segments
        -- 贝塞尔曲线公式: B(t) = (1-t)²P0 + 2(1-t)tP1 + t²P2
        local x = (1-t)*(1-t)*start.x + 2*(1-t)*t*controlPoint.x + t*t*finish.x
        local y = (1-t)*(1-t)*start.y + 2*(1-t)*t*controlPoint.y + t*t*finish.y
        table.insert(points, {x = x, y = y})
    end

    return points
end

local function init()
    deckTriangle.w = width / 10
    deckTriangle.h = height / 12
    deckTriangle.segments = 50  -- 曲线细分段数

    -- 初始化三角形顶点
    deckTriangle.xPoint.x = width
    deckTriangle.xPoint.y = height
    deckTriangle.yPoint.x = width - deckTriangle.w
    deckTriangle.yPoint.y = height
    deckTriangle.zPoint.x = width
    deckTriangle.zPoint.y = height - deckTriangle.h

    -- 计算对称点c
    deckTriangle.cPoint = love.calculateSymmetricPoint(deckTriangle.xPoint, deckTriangle.yPoint, deckTriangle.zPoint)

    -- 绘制曲线三角形
    cyCurve = generateCurve(deckTriangle.yPoint, deckTriangle.cPoint, true)
    czCurve = generateCurve(deckTriangle.zPoint, deckTriangle.cPoint, true)

    -- 构建曲线三角形顶点列表
    for i = 1, #cyCurve do
        table.insert(vertices, cyCurve[i].x)
        table.insert(vertices, cyCurve[i].y)
    end

    for i = #czCurve, 1, -1 do
        table.insert(vertices1, czCurve[i].x)
        table.insert(vertices1, czCurve[i].y)
    end
end

local function drawDeckTriangleShadow()
    setColor(1, 1, 1)
    polygon("fill", deckTriangle.cPoint.x, deckTriangle.cPoint.y, deckTriangle.yPoint.x, deckTriangle.yPoint.y, deckTriangle.zPoint.x, deckTriangle.zPoint.y)

    setColor(deckTriangle.colors.desk)
    polygon("fill", vertices1) -- B 部分
    polygon("fill", vertices) -- A 部分
end

local function drawDeckTriangle()
    -- 绘制右下角三角形
    if deckTriangle.isPressed then
        drawDeckTriangleShadow()
        setColor(deckTriangle.colors.isPressed) -- 更深的蓝灰色
    elseif deckTriangle.isHovered then
        drawDeckTriangleShadow()
        setColor(deckTriangle.colors.isHovered) -- 更浅的蓝灰色
    else
        setColor(deckTriangle.colors.normal) -- 蓝灰色
    end
    polygon("fill", deckTriangle.xPoint.x, deckTriangle.xPoint.y, deckTriangle.yPoint.x, deckTriangle.yPoint.y, deckTriangle.zPoint.x, deckTriangle.zPoint.y)
end

local function inArea(x, y)
    return x >= (width - deckTriangle.w) and y >= (height - deckTriangle.h)
end

local function isHovered(boolean)
    deckTriangle.isHovered = boolean
end

local function isPressed()
    deckTriangle.isPressed = true
end

local function isReleased()
    deckTriangle.isPressed = false
end

return{
    init = init,
    drawDeckTriangle = drawDeckTriangle,
    inArea = inArea,
    isHovered = isHovered,
    isPressed = isPressed,
    isReleased = isReleased
}